package com.appbee.cm.client.regionunit.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Singleton;

import com.appbee.cm.shared.data.ActivityEntryProxy;

@Singleton
public class ActivityEntryControl
{
	private List<ActivityEntryProxy> activityEntries;

	public List<ActivityEntryProxy> getActivityEntries()
	{
		return activityEntries;
	}

	public void setActivityEntries(List<ActivityEntryProxy> activityEntries)
	{
		this.activityEntries = activityEntries;
	}

	public List<ActivityEntryProxy> getByNavAndRegionId(Long navItemId,
														String regionId)
	{
		List<ActivityEntryProxy> list = new ArrayList<ActivityEntryProxy>();
		for (ActivityEntryProxy activityEntryProxy : activityEntries)
		{
			if (equals(navItemId, activityEntryProxy.getNavItemId())
					&& regionId.equals(activityEntryProxy.getRegionId()))
				list.add(activityEntryProxy);
		}

		return list;
	}

	public boolean equals(Long value1, Long value2)
	{
		return (value1 == null && value2 == null)
				|| (value1 != null && value1.equals(value2));
	}

	public void update(ActivityEntryProxy activityEntryProxy)
	{
		for (int i = 0; i < activityEntries.size(); i++)
		{
			ActivityEntryProxy entry = activityEntries.get(i);
			if (entry.getId().equals(activityEntryProxy.getId()))
			{
				activityEntries.set(i, activityEntryProxy);
				return;
			}
		}
		activityEntries.add(activityEntryProxy);
	}

	public void remove(ActivityEntryProxy activityEntryProxy)
	{
		activityEntries.remove(activityEntryProxy);
	}

}
